﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ET.ScoreTeacherClient.Views
{
    /// <summary>
    /// TestVoiceLibrary.xaml 的交互逻辑
    /// </summary>
    public partial class TestVoiceLibrary : Window
    {
        public TestVoiceLibrary(ObservableCollection<voicelibraryViewModel> VoicelibraryViewList)
        {
            InitializeComponent();
            this.TestVoiceLibraryList.Clear();
            TestVoiceLibraryList = VoicelibraryViewList;
        }

        public ObservableCollection<voicelibraryViewModel> TestVoiceLibraryList = new ObservableCollection<voicelibraryViewModel>();
       
        public static ObservableCollection<VoiceCommandType> CommandTypeList = new ObservableCollection<VoiceCommandType>();
        public static ObservableCollection<VoiceDevTypeName> DevTypeNameList = new ObservableCollection<VoiceDevTypeName>();
        private void VoiceSP_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;
            switch (button.Content.ToString())
            {
                case "保存":
                    string str = "";
                    if (TestVoiceLibraryList.Count>0)
                    {
                        TestVoiceLibraryDAL.DeleteTestVoiceLibrary(TestVoiceLibraryList[0].TestPaperID, TestVoiceLibraryList[0].TestQuestionID);
                        for (int i = 0; i < TestVoiceLibraryList.Count; i++)
                        {
                            if (TestVoiceLibraryList[i].IsChecked)
                            {
                                int newId = 0;
                                TestVoiceLibraryModel testVoice = new TestVoiceLibraryModel();
                                testVoice.TestPaperID = TestVoiceLibraryList[i].TestPaperID;
                                testVoice.TestQuestionID = TestVoiceLibraryList[i].TestQuestionID;
                                testVoice.VoiceLibraryID = TestVoiceLibraryList[i].ID;
                                if (TestVoiceLibraryDAL.InsertTestVoiceLibrary(out newId, testVoice))
                                {
                                    if (newId > 0)
                                    {

                                    }
                                    else
                                    {
                                        str += TestVoiceLibraryList[i].ID + "   ";
                                    }
                                }
                            }
                        }
                        if (str != "")
                        {
                            MessageBox.Show("第" + str + "存储失败！");
                        }
                        else
                        {
                            MessageBox.Show("存储成功！");
                        }
                    }
                   
                    break;
                case "取消":
                    this.Close();
                    break;
            }
            }

        private void VoiceBox_Click(object sender, RoutedEventArgs e)
        {

        }

       

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var Test = TestVoiceLibraryList.Where(p => p.IsChecked == true).ToList();
            if (Test.Count== TestVoiceLibraryList.Count)
            {
                AllCheckBox.IsChecked = true;
            }
            dgVoice.ItemsSource = null;
            dgVoice.ItemsSource = TestVoiceLibraryList;
            
            var GroupCommandList = TestVoiceLibraryList.GroupBy(x => x.CommandType).ToList();
            var GroupDevTypeNameList = TestVoiceLibraryList.GroupBy(x => x.DevTypeName).ToList();
            CommandTypeList.Clear();
            VoiceCommandType voice = new VoiceCommandType();
            voice.ID = 0;
            voice.CommandType = "全部";
            CommandTypeList.Add(voice);
            for (int i = 0; i < GroupCommandList.Count; i++)
            {
                VoiceCommandType voiceCommand = new VoiceCommandType();
                voiceCommand.ID = i+1;
                voiceCommand.CommandType = GroupCommandList[i].Key;
                CommandTypeList.Add(voiceCommand);
            }
            DevTypeNameList.Clear();
            comCommandType.ItemsSource = CommandTypeList;
            VoiceDevTypeName voiceDevType = new VoiceDevTypeName();
            voiceDevType.ID = 0;
            voiceDevType.DevTypeName = "全部";
            DevTypeNameList.Add(voiceDevType);
            for (int i = 0; i < GroupDevTypeNameList.Count; i++)
            {
                VoiceDevTypeName voiceCommand = new VoiceDevTypeName();
                voiceCommand.ID = i+1 ;
                voiceCommand.DevTypeName = GroupDevTypeNameList[i].Key;
                DevTypeNameList.Add(voiceCommand);
            }
            comDevTypeName.ItemsSource = DevTypeNameList;
        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AllCheckBox_Click(object sender, RoutedEventArgs e)
        {
          // 
            if (AllCheckBox.IsChecked== true)
            {
                TestVoiceLibraryList.ToList().ForEach(p => p.IsChecked = true);
                //dgVoice.ItemsSource = null;
                //dgVoice.ItemsSource = TestVoiceLibraryList;
            }
            else
            {
                TestVoiceLibraryList.ToList().ForEach(p => p.IsChecked = false);
                //dgVoice.ItemsSource = null;
                //dgVoice.ItemsSource = TestVoiceLibraryList;
            }
        }

        /// <summary>
        /// 命令类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comCommandType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            if (combox.SelectedItem == null) return;
            if ((comDevTypeName.SelectedItem == null || ((VoiceDevTypeName)comDevTypeName.SelectedItem).DevTypeName == "全部") &&
              ((VoiceCommandType)combox.SelectedItem).CommandType == "全部")
            {
                dgVoice.ItemsSource = TestVoiceLibraryList;
            }
            else if ((comDevTypeName.SelectedItem == null || ((VoiceDevTypeName)comDevTypeName.SelectedItem).DevTypeName == "全部") &&
              ((VoiceCommandType)combox.SelectedItem).CommandType != "全部")
            {
                string commandType = ((VoiceCommandType)combox.SelectedItem).CommandType;
                dgVoice.ItemsSource = TestVoiceLibraryList.Where(T => T.CommandType == commandType).ToList();
            }
            else if ((((VoiceDevTypeName)comDevTypeName.SelectedItem).DevTypeName != "全部") &&
             ((VoiceCommandType)combox.SelectedItem).CommandType == "全部")
            {
                string devTypeName = ((VoiceDevTypeName)comDevTypeName.SelectedItem).DevTypeName;
                dgVoice.ItemsSource = TestVoiceLibraryList.Where(T => T.DevTypeName == devTypeName).ToList();
            }
            else
            {
                string devTypeName = ((VoiceDevTypeName)comDevTypeName.SelectedItem).DevTypeName;
                var TestList = TestVoiceLibraryList.Where(T => T.DevTypeName == devTypeName).ToList();
                string commandType = ((VoiceCommandType)combox.SelectedItem).CommandType;
                dgVoice.ItemsSource = TestList.Where(T => T.CommandType == commandType).ToList();
            }
        }

        /// <summary>
        /// 设备名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comDevTypeName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            if (combox.SelectedItem == null) return;

            if ((comCommandType.SelectedItem == null|| ((VoiceCommandType)comCommandType.SelectedItem).CommandType=="全部")&&
               ((VoiceDevTypeName)combox.SelectedItem).DevTypeName == "全部")
            {
               dgVoice.ItemsSource = TestVoiceLibraryList;
            }
            else if ((comCommandType.SelectedItem == null || ((VoiceCommandType)comCommandType.SelectedItem).CommandType == "全部") &&
               ((VoiceDevTypeName)combox.SelectedItem).DevTypeName != "全部")
            {
                string devTypeName = ((VoiceDevTypeName)combox.SelectedItem).DevTypeName;
                dgVoice.ItemsSource = TestVoiceLibraryList.Where(T => T.DevTypeName == devTypeName).ToList();
            }
            else if (( ((VoiceCommandType)comCommandType.SelectedItem).CommandType != "全部") &&
              ((VoiceDevTypeName)combox.SelectedItem).DevTypeName == "全部")
            {
                string commandType = ((VoiceCommandType)comCommandType.SelectedItem).CommandType;
                dgVoice.ItemsSource = TestVoiceLibraryList.Where(T => T.CommandType == commandType).ToList();
            }
            else
            {
                string commandType = ((VoiceCommandType)comCommandType.SelectedItem).CommandType;
                var TestList = TestVoiceLibraryList.Where(T => T.CommandType == commandType).ToList();
                string devTypeName = ((VoiceDevTypeName)combox.SelectedItem).DevTypeName;
                dgVoice.ItemsSource = TestList.Where(T => T.DevTypeName == devTypeName).ToList();
            }

        }
    }
    public class VoiceCommandType
    {
       
        public int ID { get; set; }
        /// <summary>
        /// 设备名称
        /// </summary>
        public string CommandType { get; set; }
       
    }
    public class VoiceDevTypeName
    {

        public int ID { get; set; }
        /// <summary>
        /// 设备名称
        /// </summary>
        public string DevTypeName { get; set; }

    }
}
